package cn.admobiletop.adsuyi.adapter.gdt.loader;

import android.app.Activity;

import com.qq.e.ads.rewardvideo.RewardVideoAD;

import cn.admobiletop.adsuyi.ad.ADSuyiRewardVodAd;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterLoader;
import cn.admobiletop.adsuyi.ad.adapter.ADSuyiAdapterParams;
import cn.admobiletop.adsuyi.ad.data.ADSuyiPlatformPosId;
import cn.admobiletop.adsuyi.ad.listener.ADSuyiRewardVodAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.listener.RewardVodAdListener;
import cn.admobiletop.adsuyi.util.ADSuyiAdUtil;

/**
 * @author ciba
 * @description 激励视频广告加载器
 *              需要实现ADSuyiAdapterLoader<ADSuyiRewardVodAd, ADSuyiRewardVodAdListener>
 * @date 2020/4/7
 */
public class RewardVodAdLoader implements ADSuyiAdapterLoader<ADSuyiRewardVodAd, ADSuyiRewardVodAdListener> {
    private RewardVodAdListener rewardVodAdListener;

    /**
     * 加载激励视频广告
     * @param rewardVodAd
     * @param adapterParams
     * @param adListener
     */
    @Override
    public void loadAd(ADSuyiRewardVodAd rewardVodAd, ADSuyiAdapterParams adapterParams, ADSuyiRewardVodAdListener adListener) {
        if (!ADSuyiAdUtil.isReleased(rewardVodAd)
                && adapterParams != null
                && adapterParams.getPlatform() != null
                && adapterParams.getPlatformPosId() != null
                && adListener != null) {
            ADSuyiPlatformPosId adSuyiPlatformPosId = adapterParams.getPlatformPosId();
            // 激励视频广告加载的Activity
            Activity activity = rewardVodAd.getActivity();
            // 激励视频广告平台广告位ID
            String platformPosId = adSuyiPlatformPosId.getPlatformPosId();

            loadRewardVideoAD(activity, platformPosId, adListener);

        }
    }

    /**
     * 加载广点通激励视频广告
     * @param activity      广告加载广告的Activity
     * @param platformPosId 广告平台广告位ID
     * @param adListener    广告监听回调
     */
    private void loadRewardVideoAD(Activity activity, String platformPosId, ADSuyiRewardVodAdListener adListener) {
        // 构造激励视频监听类，实现广点通激励视频广告回调接口
        rewardVodAdListener = new RewardVodAdListener(platformPosId, adListener);

        // 以下是广点通加载广告逻辑
        // 构造广点通RewardVideoAD对象，传入必要的参数
        RewardVideoAD rewardVideoAD = new RewardVideoAD(activity
                , platformPosId
                , rewardVodAdListener);
        // 向封装的激励视频监听类中传入rewardVideoAD，用于返回给用户进行激励视频的播放
        rewardVodAdListener.setRewardVideoAD(rewardVideoAD);
        // 加载广点通激励视频广告
        rewardVideoAD.loadAD();
    }

    @Override
    public void onResumed() {
        // 目前不做处理
    }

    @Override
    public void onPaused() {
        // 目前不做处理
    }

    /**
     * ADSuyi广告对象被释放会调用该方法
     */
    @Override
    public void release() {
        if (rewardVodAdListener != null) {
            rewardVodAdListener.release();
            rewardVodAdListener = null;
        }
    }
}
